package com.tsmti.lab.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsmti.core.common.constant.RedisConstants;
import com.tsmti.core.common.controller.BaseCrudController;
import com.tsmti.core.common.query.data.PageJson;
import com.tsmti.core.common.query.data.Queryable;
import com.tsmti.core.util.ConvertUtil;
import com.tsmti.core.util.RedisUtils;
import com.tsmti.core.util.StringUtils;
import com.tsmti.shiro.annotation.RequiresPathPermission;
import com.tsmti.system.entity.SystemParameter;
import com.tsmti.system.entity.User;
import com.tsmti.system.service.SystemParameterService;
import com.tsmti.system.service.UserService;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author：DouDeliang
 * @Date：2018/8/22
 **/
@Controller
@RequestMapping("/system/systemParameter")
@RequiresPathPermission("system:systemParameter:list")
public class SystemParameterController extends BaseCrudController<SystemParameter, Long> {
    @Autowired
    private SystemParameterService systemParameterService;
    @Autowired
    private UserService userService;

    @Override
    public void preAjaxList(Queryable queryable, DetachedCriteria detachedCriteria, HttpServletRequest request,
                            HttpServletResponse response) {
        String paramType = ConvertUtil.null2String(request.getParameter("paramType"));
        queryable.addCondition("paramType", paramType);
    }

    /**
     * 系统参数配置显示页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/parameter*", method = RequestMethod.GET)
    public ModelAndView parameterView(HttpServletRequest request, HttpServletResponse response) {
        User user = userService.get("userName", request.getRemoteUser());
        List<SystemParameter> sysParaList = new ArrayList<SystemParameter>();
        sysParaList = systemParameterService.list();
        Map<String, String> sysMap = new HashMap<String, String>(16);
        Map<String, String> sysMapCon1 = new HashMap<String, String>(16);
        Map<String, String> sysMapCon2 = new HashMap<String, String>(16);
        Map<String, String> sysMapCon3 = new HashMap<String, String>(16);

        if (null != sysParaList && sysParaList.size() > 0) {
            for (SystemParameter systemParameter : sysParaList) {
                String paramType = systemParameter.getParamValue();
                if (null != paramType && ("1".equals(paramType))) {
                    sysMapCon1.put(systemParameter.getParamName(), systemParameter.getDefaultValue());
                } else if (null != paramType && ("2".equals(paramType))) {
                    sysMapCon2.put(systemParameter.getParamName(), systemParameter.getDefaultValue());
                } else if (null != paramType && ("3".equals(paramType))) {
                    sysMapCon3.put(systemParameter.getParamName(), systemParameter.getDefaultValue());
                }
                sysMap.put(systemParameter.getParamName(), systemParameter.getParamValue());
                sysMap.put(systemParameter.getParamName() + "Enable", ConvertUtil.null2String(systemParameter.getEnable()));
            }
        }
        ModelAndView view = new ModelAndView();
        view.addObject("sysMap", sysMap);
        view.addObject("sysMapCon1", sysMapCon1);
        view.addObject("sysMapCon2", sysMapCon2);
        view.addObject("sysMapCon3", sysMapCon3);
        return view;
    }

    /**
     * 系统参数配置保存基本信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveBase*", method = RequestMethod.POST)
    @ResponseBody
    public String parameterSaveBase(HttpServletRequest request, HttpServletResponse response) {
        JSONObject result = new JSONObject();
        return result.toString();
    }

    /**
     * 检验项目配置保存
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveExamineBase*", method = RequestMethod.POST)
    @ResponseBody
    public String examineSaveBase(HttpServletRequest request, HttpServletResponse response) {
        JSONObject result = new JSONObject();
        return result.toString();
    }

    /**
     * 条件验证配置保存
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveConditionBase*", method = RequestMethod.POST)
    @ResponseBody
    public String conditionSaveBase(HttpServletRequest request, HttpServletResponse response) {
        JSONObject result = new JSONObject();
        return result.toString();
    }

    /**
     * 申请单配置保存
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveReqNoteBase*", method = RequestMethod.POST)
    @ResponseBody
    public String reqNoteSaveBase(HttpServletRequest request, HttpServletResponse response) {
        JSONObject result = new JSONObject();
        return result.toString();
    }

    /**
     * 查询系统参数的配置值
     *
     * @return
     */
    @RequestMapping(value = "/getSystemConfig*", method = RequestMethod.GET)
    @ResponseBody
    public boolean getSystemConfig(@RequestParam(value = "systemParam") String systemParam) {
        boolean isHas = false;
        List<SystemParameter> sysParaList = new ArrayList<SystemParameter>();
        sysParaList = systemParameterService.list();
        for (SystemParameter parameter : sysParaList) {
            if (systemParam.equals(parameter.getParamName())) {
                String[] paramValue = parameter.getParamValue().split(",");
                for (String valueStr : paramValue) {
                    if (!"0".equals(valueStr)) {
                        isHas = true;
                        return isHas;
                    }
                }
            }
        }
        return isHas;
    }

    /**
     * 系统参数配置显示页面，按照参数类型分类
     *
     * @param request
     * @param response
     * @param queryable
     * @return
     */
    @RequestMapping(value = "/getList*", method = RequestMethod.GET)
    @ResponseBody
    public void getList(Queryable queryable, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String paramType = ConvertUtil.null2String(request.getParameter("paramType"));
        List<SystemParameter> list = systemParameterService.findByHql("from SystemParameter where paramType=" + paramType);
        PageJson<SystemParameter> pageJson = new PageJson<>(list);
        String content = JSON.toJSONString(pageJson);
        StringUtils.printJson(response, content);
    }


    /**
     * 增加系统配置参数
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveSystemParameter*", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveSystemParameter(HttpServletRequest request, HttpServletResponse response) {
        JSONObject result = new JSONObject();

        return result.toString();
    }

    /**
     * 增加testInfo配置
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/SaveTestInfo*", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String changeTestInfo(HttpServletRequest request, HttpServletResponse response) {
        String saveTestList = request.getParameter("saveTestList");
        JSONObject result = new JSONObject();
        return result.toString();
    }

    @Override
    public void afterSave(SystemParameter entity, HttpServletRequest request, HttpServletResponse response) {
        try {
            RedisUtils.setObject(RedisConstants.SYSTEMPARAMETER_CACHE, entity.getParamName(), entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
